//
//  ValidateText.m
//  MetroNews
//
//  Created by 郭景豪 on 2014/3/24.
//  Copyright (c) 2014年 porschemax. All rights reserved.
//

#import "ValidateText.h"

@implementation ValidateText
+ (BOOL)validateAccount:(NSString *)account{
    NSString *regex = @"[A-Z0-9a-z._%+-@]{1,30}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:account];
}

+ (BOOL)validatePassword:(NSString *)password{
    NSString *regex = @"[A-Z0-9a-z]{6,18}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    return [predicate evaluateWithObject:password];
}

+ (BOOL)validatePhone:(NSString *)phone
{
    NSString *regex = @"[0-9.+-;#*]{6,20}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:phone];
    
}
+ (BOOL)validateEmail:(NSString *)Email
{
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,5}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:Email];
    
}
+ (BOOL)validatePasswordOrEmail:(NSString *)password{
    NSString *regex = @"[A-Z0-9a-z._%+-@]{6,30}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:password];
}

+ (BOOL)validateText:(NSString *)text
{
    
    NSString *messageStr = text;
    if (messageStr !=nil) {
        messageStr = [messageStr stringByReplacingOccurrencesOfString:@" " withString:@""];
        messageStr = [messageStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    }

    if (messageStr == nil || messageStr.length==0)
    {
        return NO;
    }else
    {
        return YES;
    }
}
+ (NSString *)changeDate:(NSString *)text
{
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"/" withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@":" withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];

    NSString *staString = [NSString stringWithFormat:@"%@",text];
    NSString *parten = @"[0-9]{4,20}";
    
    NSError* error = NULL;
    
    NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:parten options:NSRegularExpressionCaseInsensitive error:&error];//正則法
    
    NSArray* match = [reg matchesInString:staString options:NSMatchingReportCompletion  range:NSMakeRange(0, [staString length])];//返回所有匹配結果的集合。
    NSString *telStrong;
    
    if (match.count != 0)
    {
        for (NSTextCheckingResult *matc in match)
        {
            NSRange range = [matc range];
            //                    NSLog(@"%lu,%lu,%@",(unsigned long)range.location,(unsigned long)range.length,[staString substringWithRange:range]);
            telStrong = [staString substringWithRange:range];
        }
        return telStrong;
    }
    return telStrong=@"";


}
+ (UIColor *) colorFromHexString:(NSString *)hexString {
    NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    if([cleanString length] == 3) {
        cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
                       [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                       [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                       [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
    }
    if([cleanString length] == 6) {
        cleanString = [cleanString stringByAppendingString:@"ff"];
    }
    
    unsigned int baseValue;
    [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
    
    float red = ((baseValue >> 24) & 0xFF)/255.0f;
    float green = ((baseValue >> 16) & 0xFF)/255.0f;
    float blue = ((baseValue >> 8) & 0xFF)/255.0f;
    float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

@end
